package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.zzyl.dto.NursingPlanProjectDto;
import com.zzyl.entity.NursingPlanProject;
import com.zzyl.mapper.NursingPlanProjectMapper;
import com.zzyl.service.NursingPlanProjectService;
import com.zzyl.vo.NursingPlanProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NursingPlanProjectServiceImpl implements NursingPlanProjectService {

    @Autowired
    private NursingPlanProjectMapper nursingPlanProjectMapper;

    /**
     * 批量操作 中间表
     * @param projectPlans
     * @param planId
     */
    @Override
    public void saveBatch(List<NursingPlanProjectDto> projectPlans, Long planId) {
//        遍历集合,把planId放入dto对象中
        for (NursingPlanProjectDto projectPlan : projectPlans) {
            projectPlan.setPlanId(planId);
        }
//        dto -> entity
        List<NursingPlanProject> list = BeanUtil.copyToList(projectPlans, NursingPlanProject.class);
//        批量保存中间表
        nursingPlanProjectMapper.insertBatch(list);
    }

    /**
     * 根据计划id查询
     * @param planId
     * @return
     */
    @Override
    public List<NursingPlanProjectVo> findByPlanId(Long planId) {
        return nursingPlanProjectMapper.selectByPlanId(planId);
    }

    /**
     * 根据计划id删除
     * @param planId
     */
    @Override
    public void deleteByPlanId(Long planId) {
        nursingPlanProjectMapper.deleteByPlanId(planId);
    }
}
